package org.ruiyichen.guizhou.agr.utils;

import android.os.Handler;
import android.text.InputFilter;
import android.text.Spanned;

import java.util.HashSet;
import java.util.Set;

/**
 * 表情过滤器
 * 作者：Administrator
 * 邮箱：wutian8489@foxmail.com
 */
public class EmojiFilter implements InputFilter {

    public final static int ILLEGAL_INPUT = 100;

    private static Set<String> filterSet = null;

    private static void addUnicodeRangeToSet(Set<String> set, int start, int end) {
        if (set == null) {
            return;
        }
        if (start > end) {
            return;
        }

        for (int i = start; i <= end; i++) {
            filterSet.add(new String(new int[] {
                    i
            }, 0, 1));
        }
    }

    private Handler handler;

    static {
        filterSet = new HashSet<>();

        // See http://apps.timwhitlock.info/emoji/tables/unicode

        // 1F601 - 1F64F
        addUnicodeRangeToSet(filterSet, 0x1F601, 0X1F64F);

        // 2702 - 27B0
        addUnicodeRangeToSet(filterSet, 0x2702, 0X27B0);

        // 1F680 - 1F6C0
        addUnicodeRangeToSet(filterSet, 0X1F680, 0X1F6C0);

        // 24C2 - 1F251
        addUnicodeRangeToSet(filterSet, 0X24C2, 0X1F251);

        // 1F600 - 1F636
        addUnicodeRangeToSet(filterSet, 0X1F600, 0X1F636);

        // 1F681 - 1F6C5
        addUnicodeRangeToSet(filterSet, 0X1F681, 0X1F6C5);

        // 1F30D - 1F567
        addUnicodeRangeToSet(filterSet, 0X1F30D, 0X1F567);

        // not included 5. Uncategorized

    }

    public EmojiFilter(Handler handler) {
        super();
        this.handler = handler;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
                               int dend) {
        // check black-rows set
        L.e("src:"+source+"| dest:"+dest);
//        if (filterSet.contains(source.toString())) {
//            if(handler!=null&&NetWorkUtils.isMostPost()){
//                handler.sendEmptyMessage(100);
//            }
//            return "";
//        }

        for (int i = start; i < end; i++) {
            int type = Character.getType(source.charAt(i));
            if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
                if (handler != null && NetWorkUtils.isMostPost()) {
                    handler.sendEmptyMessage(100);
                }
                return "";
            }
        }

        return source;
    }

}